package com.corpgovernment.supplier.vo.suppliercompany;

import com.corpgovernment.common.utils.DateUtil;
import com.corpgovernment.supplier.dto.SupplierCompanyDto;
import com.corpgovernment.supplier.entity.db.MbSupplierInfo;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * @author jhxue
 */
@Data
public class SupplierInfoVo {
    private Long id;
    /**
     * 供应商code
     */
    private String supplierCode;
    /**
     * 供应商名称
     */
    private String supplierName;
    /**
     * true表示开启，false表示停用
     */
    private Boolean isEnable;
    /**
     * true表示已经过期，false表示未过期
     */
    private Boolean isExpired;
    /**
     * 0表示全部，1表示因公，2表示因私,3表示都不选(此时必须是停用状态)
     * ALL全部，PUB，因公 ，OWN因私
     */
    private Integer busPriType;
    /**
     * logo地址
     */
    private String logoUrl;

    public static SupplierInfoVo toSupplierInfoVo(MbSupplierInfo entity) {
        SupplierInfoVo vo = new SupplierInfoVo();
        vo.setSupplierCode(entity.getSupplierCode());
        vo.setSupplierName(entity.getSupplierName());
        return vo;
    }

    public static SupplierInfoVo toSupplierInfoVo(SupplierCompanyDto dto) {
        SupplierInfoVo vo = new SupplierInfoVo();
        BeanUtils.copyProperties(dto, vo);
        Date date = Optional.ofNullable(DateUtil.stringToDate(DateUtil.dateToString(new Date(), DateUtil.DF_YMD), DateUtil.DF_YMD)).orElse(new Date());
        if (Objects.isNull(dto.getEndDate())){
            vo.setIsExpired(true);
        }else {
            vo.setIsExpired(dto.getEndDate().compareTo(date) < 0);
        }

        return vo;
    }
}
